//
//  wangQiJieXiaoViewController.m
//  YuErmuYing
//
//  Created by Tiancheng on 16/9/9.
//  Copyright © 2016年 Tiancheng. All rights reserved.
//

#import "wangQiJieXiaoViewController.h"
#import "yiYuanDuoBaoNextView.h"
#import "goodsDetailViewController.h"
#import "HttpRequest.h"
#import "goodsDetail.h"
#import "wangQiJieXiaoTableViewCell.h"
#import "wangQi.h"
#import "NSString+NSDateToNSString.h"
#import "UIImageView+WebCache.h"

@interface wangQiJieXiaoViewController ()<UITableViewDataSource,UITableViewDelegate>
@property (weak, nonatomic) IBOutlet UIView *headerView;
@property (weak, nonatomic) IBOutlet UIView *headerView01;
@property (weak, nonatomic) IBOutlet UITableView *myTableView;
@property(nonatomic,copy)NSMutableArray*dataArray;
@property(nonatomic,retain)wangQiJieXiaoTableViewCell *myCell;
@end

@implementation wangQiJieXiaoViewController
@synthesize dataArray,myCell;
-(void)viewWillAppear:(BOOL)animated
{
    self.navigationController.navigationBar.hidden=YES;
    self.tabBarController.tabBar.hidden=YES;
}
- (void)viewDidLoad
{
    [super viewDidLoad];
    //设置背景色
    [self setBackGroundColor];
    self.myTableView.delegate=self;
    self.myTableView.dataSource=self;
    dataArray=[NSMutableArray array];
    [self getDataFromNet];
}
//设置背景色
-(void)setBackGroundColor
{
    self.headerView.backgroundColor=[UIColor colorWithRed:244/255.0 green:142/255.0 blue:177/255.0 alpha:1];
    self.headerView01.backgroundColor=[UIColor colorWithRed:240/255.0 green:98/255.0 blue:146/255.0 alpha:1];
}
//从服务器获取数据
-(void)getDataFromNet
{
    goodsDetail *gd=[goodsDetail shardeManager];
    NSString*urlString=[NSString stringWithFormat:@"http://112.74.114.1/Parenting/index.php/api/indiana/review"];
    NSMutableDictionary *parameters=[NSMutableDictionary dictionary];
    parameters[@"name"]=gd.nameString;
//    NSLog(@"gd.nameString=%@",gd.nameString);
    HttpRequest *httpRequest=[HttpRequest sharedInstance];
    [httpRequest postWithURLString:urlString parameters:parameters success:^(id responseObject)
     {
         id json = [NSJSONSerialization JSONObjectWithData:responseObject options:0 error:nil];
         dataArray=[json objectForKey:@"data"];
         if (dataArray.count>0)
         {
             [self.myTableView reloadData];
         }
//         NSLog(@"json=%@",json);
     } failure:^(NSError *error) {
         NSLog(@"error=%@",error);
     }];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section;
{
    return dataArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;
{
    wangQi *wq=[wangQi shardeManager];
    static NSString *str=@"cell";
    myCell=[tableView dequeueReusableCellWithIdentifier:str];
    if (myCell==nil)
    {
        myCell=[[[NSBundle mainBundle] loadNibNamed:@"wangQiJieXiaoTableViewCell" owner:self options:nil]lastObject];
        wq.numString=[dataArray[indexPath.row] objectForKey:@"num"];
        wq.pidString=[dataArray[indexPath.row] objectForKey:@"pid"];
        wq.timeString=[dataArray[indexPath.row] objectForKey:@"time"];
        wq.uidString=[dataArray[indexPath.row] objectForKey:@"uid"];
        wq.unameString=[dataArray[indexPath.row] objectForKey:@"uname"];
        wq.upicString=[dataArray[indexPath.row] objectForKey:@"upic"];
        myCell.involveLabel.text=wq.numString;
        myCell.pidLabel.text=[NSString stringWithFormat:@"第%@期",wq.pidString];
        NSString *timeString=[NSString StringNSDateToNSString:wq.timeString];
        myCell.timeLabel.text=timeString;
        myCell.uidLabel.text=wq.uidString;
        myCell.unameLabel.text=wq.unameString;
        //图像
        NSURL* imagePath = [NSURL URLWithString:wq.upicString];
        [myCell.picImageView sd_setImageWithURL:imagePath placeholderImage:[UIImage imageNamed:@"default"]];
    }
    return myCell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath;
{
    return myCell.frame.size.height;
}
- (IBAction)goBack:(id)sender
{
    //跳到指定的界面
    for (UIViewController *controller in self.navigationController.viewControllers)
    {
        if ([controller isKindOfClass:[goodsDetailViewController class]])
        {
            [self.navigationController popToViewController:controller animated:YES];
        }
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
